$(async function () {
  var url = urlParse();
  var gid = url.gid;
  if (!gid) {
    location.href = "../indexOne.html";
  }

  var result = await searchGoodsByGoodsId({ gid });
  // console.log(result);
  var { status, data: { id, title, simg, price, head1img, colorimg, goodsprice, bigimg, smalling } } = result;
  // console.log(simg);

  if (!status) {
    location.href = "../index1.html";
  }

  var bigImgList = "";
  var bigImgList = `<li><img class="bigImgLeft" src="${bigimg}" alt=""></li>`;
  $(".bigImgList").html(bigImgList);

  var navBar = "";
  var navBar = `<li class="i"><img src="${head1img}" alt=""></li>
                 <li class="i"><img src="${colorimg}" alt=""></li>`
  $(".navBar").html(navBar);

  var bigImgBigbox = "";
  var bigImgBigbox = `<li><img class="bigImgRight" src="${bigimg}" alt=""></li>`;
  $(".bigImg-Bigbox").html(bigImgBigbox);

  var proInfos = "";
  var proInfos = `<h3 class="gooda-name">${title}</h3>
                  <p class="goods-price">${goodsprice}</p>`;
  $(".pro-infos-t").html(proInfos);

  var colorList = "";
  var colorList = `<li class="color-code"><img class="loading" src="${bigimg}" alt=""></li>`;
  $(".color-code").html(colorList);



  $(document).on("click", ".btn-addCart", async function () {

    var phone = getCookie("lgc");
    var byNum = $(".num-input").val();
    console.log(byNum);
    console.log(phone);
    if (phone) { //有用户  => 用户登录   => 加入购物车
      var result = await addToShopingCar({ phone, gid, byNum });
      console.log(result);
      var { status, msg } = result;
      if (status) {
        if (confirm("购买成功,是否进入购物车?")) {
          location.href = "cart.html";
        }
      } else {
        alert(msg);
      }
    } else {

      // 问题?
      // 用户没有登录  =>    去登录 => 登录成功(跳转主页)  

      // 期望=> 登录时,从哪里来,回哪里去?

      // 怎么解决? 跳转登录的同时,将当前网页url也作为数据传入(先编码,在传入)

      location.href = "../login.html?returnUrl=" + encodeURIComponent(location.href);
    }


  })

  //放大镜
  // $("maxLeft")
  $(".pro-bigImg").mouseenter(function () {
    $(".bigImg-sbox").show();
    $(".bigImg-Bigbox").show();
  }).mouseleave(function () {
    $(".bigImg-sbox").hide();
    $(".bigImg-Bigbox").hide();
  }).mousemove(function (e) {
    var x = e.pageX - $(this).offset().left - $(".bigImg-sbox").width() / 2;
    var y = e.pageY - $(this).offset().top - $(".bigImg-sbox").height() / 2;
    $(".bigImg-sbox").css({
      left: x + "px",
      top: y + "px",
    })
    console.log("left");
    if (parseFloat($(".bigImg-sbox").css("left")) < 0) {
      $(".bigImg-sbox").css("left", "0");
      // console.log("kk");
    } else if (parseFloat($(".bigImg-sbox").css("left")) >= parseFloat($(this).width() - $(".bigImg-sbox").width())) {
      // alert("kk");
      $(".bigImg-sbox").css("left", $(this).width() - $(".bigImg-sbox").width());
    }
    if (parseFloat($(".bigImg-sbox").css("top")) < 0) {
      $(".bigImg-sbox").css("top", "0");
    } else if (parseFloat($(".bigImg-sbox").css("top")) >= parseFloat($(this).height() - $(".bigImg-sbox").height())) {
      // alert("kk");
      $(".bigImg-sbox").css("top", $(this).height() - $(".bigImg-sbox").height());
    }
    // console.log($(".bigImg-sbox").css("background"))

    // 放大倍数

    $(".bigImgRight").css({
      left: - x * 2,
      top: - y * 2,

    });

  })

  $(".navBar li").each(function (index, item) {
    // console.log(index, item);
    $(item).click(function () {
      // console.log(this);
      $(this).removeClass("i").addClass("active").siblings().removeClass("active").addClass("i");
      var index = $(this).index();
      // console.log(index);
      $(".bigImgList li").eq(index).fadeIn().siblings().fadeOut();
      $(".bigImg-Bigbox li").eq(index).fadeIn().siblings().fadeOut();


    })
  })



})


function urlParse () {
  var search = location.search;
  var data = {};
  if (search) {
    // console.log(search);
    var str = search.substr(1);
    var list = str.split("&");
    // console.log(list);
    for (var i = 0; i < list.length; i++) {
      var item = list[i];
      var key = item.split("=")[0];
      var val = item.split("=")[1];
      data[key] = val;
    }
    // console.log(data);
  }
  return data;
}














